package cn.iocoder.yudao.server.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.sign.AESUtil;
import cn.iocoder.yudao.framework.common.util.sign.SignUtil;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.JdApiEncryptReqVO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.JdApiNatureReqVO;
import com.alibaba.fastjson.JSONArray;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;

/**
 * 默认 Controller，解决部分 module 未开启时的 404 提示。
 * 例如说，/bpm/** 路径，工作流
 *
 * @author 芋道源码
 */
@RestController
public class DefaultController {

    @RequestMapping("/admin-api/bpm/**")
    public CommonResult<Boolean> bpm404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[工作流模块 yudao-module-bpm - 已禁用][参考 https://doc.iocoder.cn/bpm/ 开启]");
    }

    @RequestMapping("/admin-api/mp/**")
    public CommonResult<Boolean> mp404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[微信公众号 yudao-module-mp - 已禁用][参考 https://doc.iocoder.cn/mp/build/ 开启]");
    }

    @RequestMapping(value = {"/admin-api/product/**", // 商品中心
            "/admin-api/trade/**", // 交易中心
            "/admin-api/promotion/**"})  // 营销中心
    public CommonResult<Boolean> mall404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[商城系统 yudao-module-mall - 已禁用][参考 https://doc.iocoder.cn/mall/build/ 开启]");
    }

    @RequestMapping("/admin-api/erp/**")
    public CommonResult<Boolean> erp404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[ERP 模块 yudao-module-erp - 已禁用][参考 https://doc.iocoder.cn/erp/build/ 开启]");
    }

    @RequestMapping("/admin-api/crm/**")
    public CommonResult<Boolean> crm404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[CRM 模块 yudao-module-crm - 已禁用][参考 https://doc.iocoder.cn/crm/build/ 开启]");
    }

    @RequestMapping(value = {"/admin-api/report/**"})
    public CommonResult<Boolean> report404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[报表模块 yudao-module-report - 已禁用][参考 https://doc.iocoder.cn/report/ 开启]");
    }

    @RequestMapping(value = {"/admin-api/pay/**"})
    public CommonResult<Boolean> pay404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[支付模块 yudao-module-pay - 已禁用][参考 https://doc.iocoder.cn/pay/build/ 开启]");
    }

    @RequestMapping(value = {"/admin-api/ai/**"})
    public CommonResult<Boolean> ai404() {
        return CommonResult.error(NOT_IMPLEMENTED.getCode(),
                "[AI 大模型 yudao-module-ai - 已禁用][参考 https://doc.iocoder.cn/ai/build/ 开启]");
    }

    /**
     * 验证签名
     */
    @PostMapping("/checkJdSignature")
    @Operation(summary = "验证签名")
//    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public boolean checkJdSignature(@RequestBody JdApiNatureReqVO jdApiNatureReqVO) {
        String sourceNature = jdApiNatureReqVO.getSign();
        //签名有序性，key以字母升序排列
        Map<String, Object> treeMap = SignUtil.convertToTreeMap(jdApiNatureReqVO);
        //构造签名
        String nature= SignUtil.buildSignature(treeMap);
        //签名比对
        return nature.equals(sourceNature);
    }

    /**
     * 验证签名
     */
    @PostMapping("/checkSignature")
    @Operation(summary = "验证签名")
//    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public boolean checkSignature(@RequestBody  PreInfoItem infoItem) {
        String sourceNature = infoItem.getSign();
        //签名有序性，key以字母升序排列
        Map<String, Object> treeMap = SignUtil.convertToTreeMap(infoItem);
        //构造签名
        String nature= SignUtil.buildSignature(treeMap);
        //签名比对
        return nature.equals(sourceNature);
    }

    /**
     * 卡密加密
     */
    @PostMapping("/encrypt")
    @Operation(summary = "验证签名")
//    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public CommonResult<String> encrypt(@RequestBody ArrayList<JdApiEncryptReqVO> list) throws Exception {
//        JSONArray jsonObject = (JSONArray)JSON.toJSON(list);//会序列化参数，导致参数的顺序变化
//        String jsonString = jsonObject.toJSONString();
        //转成json字符串,保持原参数的格式，toString会使参数和输入参数格式不一样。
        String jsonString = JSONArray.toJSONString(list);
        System.out.println("jsonString: " + jsonString);
        //todo 密钥有业务人员分配，从配置文件读取。
        String key = "12345678901234567890123456789012";
        String ciphertext = AESUtil.encrypt(jsonString, key);
        System.out.println("Ciphertext: " + ciphertext);
        String decryptedText = AESUtil.decrypt(ciphertext, key);
        System.out.println("Decrypted text: " + decryptedText);
        return CommonResult.success(ciphertext);
    }

    @PostMapping("/test")
    @Operation(summary = "测试接口是否可以访问")
    public String test()  {
        return "成功";
    }
}